#include "audio.h"


//==============================================================================DATA
#if 0
typedef struct output{
	void *handle;
	int (*open)();
	int (*close)();
	int (*config)( entry_s *ety );
	int (*operate)( entry_s *ety, control_e control, status_s *status );
	bool (*do_you_need_to_decode)();
}output_s;
#endif
output_s output = {NULL};
char *output_using = NULL;


static int output_install( char *output_name );
static void output_uninstall();
//==============================================================================FUNCTION
int plugin_output_install()
{
	int e=0, i=0;

	for( i=0 ; context.libs_output[i] != NULL ; i++ ){
		if( ! strcmp( context.libs_output[i], output_using ) ) break;
	}
	if( context.libs_output[i] == NULL && context.libs_output[0] != NULL ) 
		output_using = context.libs_output[0];
	
	if( output_using ){
		if( -1 == output_install( output_using ) )
			return EXIT_FAILURE;
	}else return EXIT_FAILURE;

	return EXIT_SUCCESS;
}

void plugin_output_uninstall()
{
	if(output.handle) output_uninstall();
}

//------------------------------------------------------------------------------
static int output_install( char *output_name )
{
	void *handle = NULL, *func = NULL;

	char _output_name[512] = "\0";
	strcpy( _output_name, context.lib_dir );
	strcat( _output_name, output_name );
	
	handle = dlopen( _output_name, RTLD_NOW | RTLD_GLOBAL);
	if( handle ) output.handle = handle;
	else goto error;

	func = dlsym( handle, "openup" );
	if( func == NULL ) goto error;
	else output.open = func;
	func = NULL;

	func = dlsym( handle, "closedown" );
	if( func == NULL ) goto error;
	else output.close = func;
	func = NULL;

	func = dlsym( handle, "config" );
	if( func == NULL ) goto error;
	else output.config = func;
	func = NULL;

	func = dlsym( handle, "operate" );
	if( func == NULL ) goto error;
	else output.operate = func;

	func = dlsym( handle, "do_you_need_to_decode" );
	if( func == NULL ) goto error;
	else output.do_you_need_to_decode = func;

	return EXIT_SUCCESS;
	
error:
	printf( "\nERROR: %s", dlerror() );
	return EXIT_FAILURE;
}

static void output_uninstall()
{
	if( output.handle ) dlclose( output.handle );
}
